package q1;

import java.util.HashMap;

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int i1 = 0; i1 < nums.length; i1++) {
                if (nums[i] + nums[i1] == target && i != i1) {
                    return new int[]{i, i1};
                }
            }
        }
        return new int[]{0, 0};
    }


    public int[] twoSum1(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int one = nums[i];

            for (int j = i + 1; j < n; j++) {
                if (one + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }


    //利用数据结构，减少时间复杂度。
    public int[] twoSum3(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int value1 = nums[i];
            int value2 = target - value1;
            if (map.containsKey(value2)) {
                return new int[]{i, map.get(value2)};
            }
            map.put(value1, i);
        }
        return null;
    }
}